import time

from django.db import models
from django.core.validators import MinLengthValidator

class User(models.Model):
    username = models.CharField(
        max_length=50,
        unique=True,
        validators=[MinLengthValidator(4)]
    )
    password = models.CharField(max_length=128)
    email = models.EmailField(blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        abstract = True

    def __str__(self):
        return self.username

class Admin(User):
    ADMIN_LEVEL_CHOICES = [
        ('super', '超级管理员'),
        ('content', '内容管理员'),
        ('audit', '审核管理员'),
    ]
    admin_level = models.CharField(
        max_length=10,
        choices=ADMIN_LEVEL_CHOICES,
        verbose_name="管理员级别",
        blank=True,
        null=True
    )
    last_login_ip = models.GenericIPAddressField(
        blank=True,
        null=True,
        verbose_name="最后登录IP"
    )

    class Meta:
        verbose_name = '管理员'
        verbose_name_plural = '管理员'

class CultureAdmin(User):
    PROVINCE_CHOICES = [
        ('11', '北京市'), ('12', '天津市'), ('13', '河北省'),
        ('14', '山西省'), ('15', '内蒙古自治区'), ('21', '辽宁省'),
        ('22', '吉林省'), ('23', '黑龙江省'), ('31', '上海市'),
        ('32', '江苏省'), ('33', '浙江省'), ('34', '安徽省'),
        ('35', '福建省'), ('36', '江西省'), ('37', '山东省'),
        ('41', '河南省'), ('42', '湖北省'), ('43', '湖南省'),
        ('44', '广东省'), ('45', '广西壮族自治区'), ('46', '海南省'),
        ('50', '重庆市'), ('51', '四川省'), ('52', '贵州省'),
        ('53', '云南省'), ('54', '西藏自治区'), ('61', '陕西省'),
        ('62', '甘肃省'), ('63', '青海省'), ('64', '宁夏回族自治区'),
        ('65', '新疆维吾尔自治区'), ('81', '香港特别行政区'),
        ('82', '澳门特别行政区'), ('71', '台湾省'),
    ]

    VERIFICATION_STATUS = (
        ('pending', '待审核'),
        ('approved', '已通过'),
        ('rejected', '已拒绝'),
    )

    organization = models.CharField(
        max_length=100,
        verbose_name="文旅机构名称",
        blank=True,
        null=True
    )
    license_number = models.CharField(
        max_length=50,
        verbose_name="文旅许可证号",
        blank=True,
        null=True
    )
    province = models.CharField(
        max_length=2,
        choices=PROVINCE_CHOICES,
        verbose_name="管辖省级区域",
        blank=True,
        null=True
    )
    contact_phone = models.CharField(
        max_length=15,
        verbose_name="联系电话",
        blank=True,
        null=True
    )
    verification_status = models.CharField(
        max_length=10,
        choices=VERIFICATION_STATUS,
        default='pending',
        verbose_name="审核状态"
    )
    rejection_reason = models.TextField(
        verbose_name="拒绝原因",
        blank=True,
        null=True
    )
    verified_at = models.DateTimeField(
        verbose_name="审核时间",
        blank=True,
        null=True
    )

    def is_verified(self):
        return self.verification_status == 'approved'

    class Meta:
        verbose_name = '文旅管理员'
        verbose_name_plural = '文旅管理员'

class RegularUser(User):
    GENDER_CHOICES = [
        ('M', '男'), ('F', '女'),
        ('O', '其他'), ('U', '未知'),
    ]

    avatar_timestamp = models.IntegerField(default=0)

    def save(self, *args, **kwargs):
        if self.avatar:  # 如果更新了头像
            self.avatar_timestamp = int(time.time())
        super().save(*args, **kwargs)
    # 基本信息
    nickname = models.CharField(
        max_length=50,
        verbose_name="昵称",
        blank=True,
        null=True
    )
    gender = models.CharField(
        max_length=1,
        choices=GENDER_CHOICES,
        default='U',
        verbose_name="性别",
        blank=True,
        null=True
    )
    birth_date = models.DateField(
        verbose_name="出生日期",
        blank=True,
        null=True
    )
    avatar = models.ImageField(
        upload_to='avatars/',
        verbose_name="头像",
        blank=True,
        null=True
    )

    # 地区信息
    province = models.CharField(
        max_length=20,
        verbose_name="省份",
        blank=True,
        null=True
    )
    city = models.CharField(
        max_length=20,
        verbose_name="城市",
        blank=True,
        null=True
    )
    district = models.CharField(
        max_length=20,
        verbose_name="区县",
        blank=True,
        null=True
    )

    # 兴趣标签
    INTEREST_CHOICES = [
        ('history', '历史人文'), ('nature', '自然风光'),
        ('food', '美食探店'), ('shopping', '购物'),
        ('sports', '户外运动'), ('art', '艺术展览'),
        ('music', '音乐演出'), ('family', '亲子活动'),
    ]
    interests = models.CharField(
        max_length=100,
        verbose_name="兴趣标签",
        help_text="多个兴趣用逗号分隔",
        blank=True,
        null=True
    )

    # 行为参数
    travel_frequency = models.PositiveSmallIntegerField(
        verbose_name="年旅游次数",
        help_text="每年大约旅游次数",
        default=0,
        blank=True,
        null=True
    )
    budget_preference = models.CharField(
        max_length=10,
        choices=[
            ('low', '经济型'),
            ('medium', '舒适型'),
            ('high', '豪华型'),
        ],
        default='medium',
        verbose_name="消费偏好",
        blank=True,
        null=True
    )

    # 社交信息
    wechat_id = models.CharField(
        max_length=50,
        verbose_name="微信ID",
        blank=True,
        null=True
    )
    bio = models.TextField(
        max_length=500,
        verbose_name="个人简介",
        blank=True,
        null=True
    )

    class Meta:
        verbose_name = "普通用户"
        verbose_name_plural = "普通用户管理"